﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace smartphone.UserControls
{
    public struct PageUrl
    {
        private string page;
        private string url;

        public PageUrl(string page, string url)
        {
            this.page = page;
            this.url = url;
        }

        public string Page
        {
            get
            {
                return page;
            }
        }

        public string Url
        {
            get
            {
                return url;
            }
        }
    }

    public partial class Pager : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void Show_Pager(int currentPage, int howManyPages, string firstPageUrl,
            string pageUrlFormat, bool showPages)
        {
            if (howManyPages > 1)
            {
                this.Visible = true;
                //label trang hien tai va tong so trang
                //lbCurrentPage.Text = currentPage.ToString();
                //lbHowManyPage.Text = howManyPages.ToString();

                //hien thi button previous page
                if (currentPage == 1)
                {
                    previousLink.Enabled = false;
                }
                else
                {
                    previousLink.NavigateUrl = (currentPage == 2) ? 
                        firstPageUrl : String.Format(pageUrlFormat, currentPage - 1);
                }

                //hien thi button lastpage
                if (currentPage == howManyPages)
                {
                    nextLink.Enabled = false;
                }
                else
                {
                    nextLink.NavigateUrl = String.Format(pageUrlFormat, currentPage + 1);
                }

                if (showPages)
                {
                    PageUrl[] pages = new PageUrl[howManyPages];
                    pages[0] = new PageUrl("1", firstPageUrl);

                    for (int i = 2; i <= howManyPages; i++)
                    {
                        pages[i - 1] = new PageUrl(i.ToString(), String.Format(pageUrlFormat, i));
                    }
                    pages[currentPage - 1] = new PageUrl((currentPage-1).ToString(), "");

                    pagesRepeater.DataSource = pages;
                    pagesRepeater.DataBind();
                }
            }
        }
    }
}